#include<iostream>
#include<string>
using namespace std;

int main()
{
    string str; cin >> str;
    str = " " + str;
    int n; cin >> n;
    while (n--)
    {
        int s, e; cin >> s >> e;
        string st, en; cin >> st >> en;
        string tmp = str.substr(s , e - s + 1);
        // cout << "tmp : " << tmp << endl;
        str.erase(s, e - s + 1);
        int p1 = str.find(st, 0), p2 = str.find(en, p1);
        if ((p1 == -1 || p2 == -1) || p1 + st.size() != p2) str += tmp;
        else str.insert(p1 + st.size(), tmp);
        cout << "p1 : " << p1 << " p2 : " << p2 << endl;
        cout << "str after insert: " << str << endl;

    }
    str = str.substr(1);
    cout << str << endl;
    return 0;
}